package com.macro.mall.portal.service.impl;

import com.macro.mall.mapper.BmsNarratorPicMapper;
import com.macro.mall.model.BmsNarratorPic;
import com.macro.mall.model.BmsNarratorPicExample;
import com.macro.mall.portal.service.BmsNarratorPicService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class BmsNarratorPicServiceImpl implements BmsNarratorPicService {

    @Autowired
    private BmsNarratorPicMapper bmsNarratorPicMapper;

    @Override
    public void create(Long narratorId, String coverPic, List<String> picUrlList) {

        BmsNarratorPic itemObject;

        if (StringUtils.isNotBlank(coverPic)) {
            itemObject = new BmsNarratorPic();
            itemObject.setPic(coverPic);
            itemObject.setNarratorId(narratorId);
            itemObject.setIsCover((byte) 1);
            bmsNarratorPicMapper.insert(itemObject);
        }

        if (CollectionUtils.isNotEmpty(picUrlList)) {
            for (String pic : picUrlList) {
                itemObject = new BmsNarratorPic();
                itemObject.setPic(pic);
                itemObject.setNarratorId(narratorId);
                itemObject.setIsCover((byte) 0);
                bmsNarratorPicMapper.insert(itemObject);
            }
        }
    }

    @Override
    public void deleteByActivitiesId(Long narratorId) {
        BmsNarratorPicExample example = new BmsNarratorPicExample();
        BmsNarratorPicExample.Criteria criteria = example.createCriteria();
        criteria.andNarratorIdEqualTo(narratorId);
        bmsNarratorPicMapper.deleteByExample(example);
    }

    @Override
    public String findCoverPicByActivitiesId(Long narratorId) {
        BmsNarratorPicExample example = new BmsNarratorPicExample();
        BmsNarratorPicExample.Criteria criteria = example.createCriteria();
        criteria.andNarratorIdEqualTo(narratorId);
        criteria.andIsCoverEqualTo((byte) 1);
        List<BmsNarratorPic> bmsNarratorPics = bmsNarratorPicMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(bmsNarratorPics)) {
            return bmsNarratorPics.get(0).getPic();
        }
        return null;
    }

    @Override
    public List<String> findPicListByActivitiesId(Long narratorId) {
        BmsNarratorPicExample example = new BmsNarratorPicExample();
        BmsNarratorPicExample.Criteria criteria = example.createCriteria();
        criteria.andNarratorIdEqualTo(narratorId);
        criteria.andIsCoverEqualTo((byte) 0);
        List<BmsNarratorPic> bmsNarratorPics = bmsNarratorPicMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(bmsNarratorPics)) {
            return bmsNarratorPics.stream().map(BmsNarratorPic::getPic).collect(Collectors.toList());
        }
        return null;
    }
}
